#pragma once
#include <vector>
#include <list>
#include <QThread>
#include <QMutex>
#include <QTimer>
#include <QWidget>
#include <QBoxLayout>

#include <exception>
#include "Filter.h"

class ProcessingGraph :
	public QThread
{
	Q_OBJECT

public:
	class ProcessingGraphException : public std::exception
	{
	public:
		ProcessingGraphException(const char *message) : std::exception(message)	{}
	};

	ProcessingGraph(void);
	~ProcessingGraph(void);

	void AddFilter(Filter &filter);
	void Compile();
	QWidget *GetWidget();

signals:
	void Fps(const QString &fps);
	void EndOfStream();

public slots:
	void Start();
	void Stop();
	void LoadFromXML(const QString &filename);
	void SaveToXML(const QString &filename);

private slots:
	void SendFPS();

private:
	virtual void run();
	typedef std::vector<Filter*> FiltersVector;
	typedef std::list<Filter*> FiltersList;
	FiltersVector filters;
	FiltersList computingOrder;

	bool running;
	QMutex runningMutex;

	QTimer fpsTimer;

	QMutex framesMutex;
	int frames;

	//UI
	QWidget *widget;
	QHBoxLayout *widgetLayout;
	QHBoxLayout *filtersLayout;
	QVBoxLayout *propertiesLayout;
	
};
